
var setting = {
		view: {
			selectedMulti: false,//多选
			showLine: true,
		},
		async: {
			enable: true,
			url:"../Department/deptTreeNode",
			autoParam:["id"],
			otherParam:{"otherParam":"zTreeAsyncTest"},//提交的参数[key：value]
			dataFilter: filter
		},
		callback: {
			beforeClick: beforeClick,
			beforeAsync: beforeAsync,
			onAsyncError: onAsyncError,
			onAsyncSuccess: onAsyncSuccess
		},data:{
			simpleData:{
				enable: true,
			}
		}
	};

	function filter(treeId, parentNode, childNodes) {
		if (!childNodes) return null;
		for (var i=0, l=childNodes.length; i<l; i++) {
			
			childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
		}
		return childNodes;
	}
	function beforeClick(treeId, treeNode) {
		//alert(treeNode.id);
		window.parent.frames['show'].location="../Department/dept?id="+treeNode.id;
		//"dept?id="+treeNode.id
		return true;
	}
	//var log, className = "dark";
	function beforeAsync(treeId, treeNode) {
		
		//className = (className === "dark" ? "":"dark");
		//showLog("[ "+getTime()+" beforeAsync ]&nbsp;&nbsp;&nbsp;&nbsp;" + ((!!treeNode && !!treeNode.name) ? treeNode.name : "root") );
		return true;
	}
	function onAsyncError(event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown) {
		//showLog("[ "+getTime()+" onAsyncError ]&nbsp;&nbsp;&nbsp;&nbsp;" + ((!!treeNode && !!treeNode.name) ? treeNode.name : "root") );
	}
	function onAsyncSuccess(event, treeId, treeNode, msg) {
		
		//showLog("[ "+getTime()+" onAsyncSuccess ]&nbsp;&nbsp;&nbsp;&nbsp;" + ((!!treeNode && !!treeNode.name) ? treeNode.name : "root") );
	}
	
	function getTime() {
		var now= new Date(),
		h=now.getHours(),
		m=now.getMinutes(),
		s=now.getSeconds(),
		ms=now.getMilliseconds();
		return (h+":"+m+":"+s+ " " +ms);
	}

	//刷新选择节点
	/*
	function refreshNode(e) {
		var zTree = $.fn.zTree.getZTreeObj("deptTree"),
		type = e.data.type,
		silent = e.data.silent,
		nodes = zTree.getSelectedNodes();
		if (nodes.length == 0) {
			alert("请先选择一个父节点");
		}
		for (var i=0, l=nodes.length; i<l; i++) {
			zTree.reAsyncChildNodes(nodes[i], type, silent);
			if (!silent) zTree.selectNode(nodes[i]);
		}
	}*/
	function testframeset(id){ alert(id);}
	
	function addTreeNode(e,t){
		$(function(){ 
            $.ajax({ 
                async: false, 
                cache: false, 
                type: 'POST', 
                dataType:"html", 
                url: '../Department/addtree?'+t,
                success: function(data){ 
                	//成功刷新父节点
                	var zTree = $.fn.zTree.getZTreeObj("deptTree"),
            		nodes = zTree.getSelectedNodes();
                	
                	for (var i=0, l=nodes.length; i<l; i++) {
            			zTree.reAsyncChildNodes(nodes[i], "refresh", true);
            			if (!silent) zTree.selectNode(nodes[i]);
            		}
                },error: function(){ 
                	//失败
                    alert( "添加失败" ); 
                } 
            }); 
        });

	}
	function deleteTreeNode(){
		
	}
	function updataTreeNode(){
		
	}
	function refTreeNode(){
		var zTree = $.fn.zTree.getZTreeObj("deptTree"),
		nodes = zTree.getSelectedNodes();
		//alert(nodes[0].isParent);
		if (!nodes[0].isParent) {
			alert("请先选择一个父节点刷新");
			return ;
		}
		for (var i=0, l=nodes.length; i<l; i++) {
			zTree.reAsyncChildNodes(nodes[i], "refresh", true);
			if (!silent) zTree.selectNode(nodes[i]);
		}
	}
	//给id 刷新这个节点 刷新后选中seclectid
	function refparentTreeNode(id,seclectid){
		//alert("传递："+id);
		var zTree = $.fn.zTree.getZTreeObj("deptTree"),
		nodes=zTree.getNodeByParam("id",id,null);
		//alert("获取："+nodes.id);
		zTree.reAsyncChildNodes(nodes, "refresh", true);//刷新 父节点
		zTree.selectNode(zTree.getNodeByParam("id",seclectid,null));
		//选中删除的相邻节点
		
	}
	//重新载入树
	function loadDeptTreeNode(){
		//alert(1);
		
		//alert(2);
	}
	//初始化时默认第一个选择
	function loadTreenode(){
		
		//alert(1)
		window.parent.frames['show'].location='../Department/dept?id=1';
		var zTree = $.fn.zTree.getZTreeObj("deptTree"),nodes = zTree.getNodes();
		if (nodes.length>0) {
			zTree.selectNode(nodes[0],false);
		}
	}
	//获取选择的节点的
	function getTreeNode(){
		var zTree = $.fn.zTree.getZTreeObj("deptTree"),
		nodes = zTree.getSelectedNodes();
		//alert(nodes[0].id)
		return nodes[0];
	}//获取选择的相邻的节点
	function getTreeNodenext(){
		//alert(getTreeNode().getNextNode().id);
		return getTreeNode().getNextNode()==null?getTreeNode().getPreNode():getTreeNode().getNextNode();
	}
	function loadEmpTreeNode(){
		window.parent.frames['show'].location='Staff.jsp';
	}
	var isNum = /^[0-9]*$/;
	var settingEmp = {
			view : {
				selectedMulti : false,//多选
				showLine : true,//显示链接线
				txtSelectedEnable :true,//可以选择文字
				showIcon : true,//显示ico
				dblClickExpand: true,//允许双击展开关闭节点
				fontCss : {color:"#1B9AF7"},
				nameIsHTML: true

			},
			async : {
				enable : true,
				url : "../emp/loademp",
				autoParam : [ "id" ],
				otherParam : {
					"otherParam" : "zTreeAsyncTest"
				},//提交的参数[key：value]
				dataFilter : filteremp
			},
			callback : {
				beforeClick: beforeClickemp,
				beforeAsync : beforeAsync,
				onAsyncError : onAsyncError,
				onAsyncSuccess : onAsyncSuccessemp
			},
			data : {
				simpleData : {
					enable : true,
				}
			}
		};
	function filteremp(treeId, parentNode, childNodes) {
		if (!childNodes) return null;
		for (var i=0, l=childNodes.length; i<l; i++) {
			
			childNodes[i].name = '['+childNodes[i].name.replace(/\.n/g, '.')+']';
		}
		return childNodes;
	}
	function onAsyncSuccessemp(event, treeId, treeNode, msg) {
		var treeObj = $.fn.zTree.getZTreeObj(treeId);
		var nodes = treeObj.transformToArray(treeObj.getNodes());
		//alert(nodes.length);
		for(var i=0;i<nodes.length;i++){
			if(isNum.test(nodes[i].id+"")){
				nodes[i].isParent=true;
				treeObj.updateNode(nodes[i]);
			}
			if(nodes[i].id+""=="0"){
				nodes[i].name="<span style='color:red;margin-right:0px;'>"+nodes[i].name+"</span>";
				
				treeObj.updateNode(nodes[i]);
				treeObj.expandNode(nodes[i], true, false, true);
			}
		}
		var node = treeObj.getNodeByParam("id", 10, null);//选中节点部门id 为10的节点
		treeObj.selectNode(node);
	}
	function beforeClickemp(treeId, treeNode) {
		
		if(!isNum.test(treeNode.id+"")&&!treeNode.isParent){//不是数字且不是父节点
			window.parent.frames['show'].location="../emp/loaduser?accountarray="+treeNode.id;
		}else if((treeNode.id+"")=="50") {
			window.parent.frames['show'].location="../emp/emp?empid="+(treeNode.id*1+1);
		}else if((treeNode.id+"")=="0"){
			window.parent.frames['show'].location="../examples/msg.jsp";
		}else window.parent.frames['show'].location="../emp/emp?empid="+treeNode.id;
		return true;
	}
	//刷新树
	function refempTreenode(){

		$.fn.zTree.destroy("emptreeNode");
		$.fn.zTree.init($("#emptreeNode"), settingEmp);
	}
	$(document).ready(function(){
		//alert("${path}");
		$.fn.zTree.init($("#deptTree"), setting);
		$.fn.zTree.init($("#emptreeNode"), settingEmp);
		
	});
